<?php

namespace app\common\model;

use think\Model;

/**
 * Tags
 */
class Tags extends Model
{
    // 表名
    protected $name = 'tags';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;
    protected $updateTime = false;
    protected $append = ['domain_image'];

    public function getDomainImageAttr($value, $data)
    {
        if (isset($data['image'])) {
            return request()->domain() . $data['image'];
        } else {
            return '';
        }
    }

    protected static function onAfterInsert($model)
    {
        if ($model->weigh == 0) {
            $pk = $model->getPk();
            if (strlen($model[$pk]) >= 19) {
                $model->where($pk, $model[$pk])->update(['weigh' => $model->count()]);
            } else {
                $model->where($pk, $model[$pk])->update(['weigh' => $model[$pk]]);
            }
        }
    }
}
